package com.idp.system.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.idp.common.base.BaseController;
import com.idp.common.model.PageModel;
import com.idp.common.model.ResultModel;
import com.idp.system.dto.SysDictionaryDto;
import com.idp.system.dto.SysDictionarySearchDto;
import com.idp.system.service.SysDictionaryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 数据字典Controller
 * @author King
 */
@Api(tags="数据字典")
@RestController
@RequestMapping("/dictionary/")
public class SysDictionaryController extends BaseController {

	@Autowired
	private SysDictionaryService sysDictionaryService;

	/**
	 * 分页查询
	 * @param sysDictionarySearchDto
	 * @return
	 */
	@ApiOperation("分页查询")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "page", value = "页数", paramType = "query", required = true, defaultValue = "1"),
			@ApiImplicitParam(name = "rows", value = "每页行数", paramType = "query", required = true, defaultValue = "10")
	})
	@PostMapping("findByPage")
	public ResultModel<PageModel<SysDictionaryDto>> findByPage(@RequestBody SysDictionarySearchDto sysDictionarySearchDto) {
		return ResultModel.success(sysDictionaryService.findByPage(sysDictionarySearchDto));
	}

	/**
	 * 根据id查询字典表
	 * @param sysDictionaryDto
	 * @return
	 */
	@ApiOperation("根据id查询字典表")
	@ApiOperationSupport(includeParameters = {"sysDictionaryDto.id"})
	@PostMapping("findById")
	public ResultModel<SysDictionaryDto> findById(@RequestBody SysDictionaryDto sysDictionaryDto) {
		return ResultModel.success(sysDictionaryService.getById(sysDictionaryDto.getId()));
	}

	/**
	 * 新增数据字典
	 * @param sysDictionaryDto
	 * @return
	 */
	@ApiOperation("新增")
	@ApiOperationSupport(ignoreParameters = {"sysDictionaryDto.id"})
	@PostMapping("add")
	public ResultModel add(@RequestBody SysDictionaryDto sysDictionaryDto) {
		sysDictionaryService.add(sysDictionaryDto);
		return ResultModel.success("新增成功", null);
	}

	/**
	 * 新增数据字典
	 * @param sysDictionaryDto
	 * @return
	 */
	@ApiOperation("新增1")
	@PostMapping("save")
	public ResultModel save(@RequestBody SysDictionaryDto sysDictionaryDto) {
		sysDictionaryService.save(sysDictionaryDto);
		return ResultModel.success("新增成功", null);
	}

	/**
	 * 修改数据字典
	 * @param sysDictionaryDto
	 * @return
	 */
	@ApiOperation("修改数据字典")
	@PostMapping("update")
	public ResultModel update(@RequestBody SysDictionaryDto sysDictionaryDto) {
		sysDictionaryService.update(sysDictionaryDto);
		return ResultModel.success("修改成功", null);
	}

	/**
	 * 根据code查询数据字典明细
	 * @param sysDictionaryDto
	 * @return
	 */
	@ApiOperation("根据code查询数据字典明细")
	@ApiOperationSupport(includeParameters = {"sysDictionaryDto.dictCode"})
	@PostMapping("selectDictionaryByCode")
	public ResultModel<List<SysDictionaryDto>> selectDictionaryByCode(@RequestBody SysDictionaryDto sysDictionaryDto) {
		return ResultModel.success(sysDictionaryService.selectDictionaryByCode(sysDictionaryDto.getDictCode()));
	}

	/**
	 * 查询所有数据字典
	 * @return
	 */
	@ApiOperation("查询所有数据字典")
	@PostMapping("findAll")
	public ResultModel<List<SysDictionaryDto>> findAll() {
		return ResultModel.success(sysDictionaryService.findAll());
	}



	/**
	 * 删除
	 *
	 * @param sysDictionaryDto
	 * @return
	 */
	@ApiOperation(value = "删除")
	@PostMapping("delete")
	public ResultModel delete(@RequestBody SysDictionaryDto sysDictionaryDto) {
		sysDictionaryService.delete(sysDictionaryDto.getId());
		return ResultModel.success("删除成功", null);
	}

}
